//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/.
// 			VisualNovelToolkit		     /_/_/_/_/_/_/_/_/_/.
// Copyright ©2013 - Sol-tribe.	/_/_/_/_/_/_/_/_/_/.
//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/.
using UnityEngine;
using System.Collections;
using ViNoToolkit;

/// <summary>
/// Continue event script attached to save (load ) button.
/// </summary>
public class ContinueEvent : MonoBehaviour {
	public string fileName;

	public ViNoTextEventListener dateTextEvtLis;
	public ViNoTextEventListener descriptionTextEvtLis;

	[HideInInspector] public ViNoSaveInfo info;

	private bool m_FileExists = false;

	void UpdateText( string dateStr , string desc ){
//		ViNoSaveInfo info = ScenarioCtrl.Instance.saveInfo;
		// Update Text View.
		if( dateTextEvtLis != null && descriptionTextEvtLis != null ){
			dateTextEvtLis.OnUpdateText( dateStr );
			descriptionTextEvtLis.OnUpdateText( desc );
		}
	}

	void Start(){
		if( info == null ){
			info = ScriptableObject.CreateInstance<ViNoSaveInfo>();
		}

		m_FileExists = ExternalAccessor.IsSaveDataFileExists( fileName );
		if( m_FileExists ){
			// Load SaveInfo from Storage.
			ViNo.LoadDataFromStorage( fileName , ref info) ;
			UpdateText( info.m_Date , info.m_ScenarioDescription );
		}
		else{
			UpdateText( "" , "NO DATA" );
		}
	}

	void DoSave( bool t ){
		if( t ){
			ViNo.SaveData( fileName , info );
			UpdateText( info.m_Date , info.m_ScenarioDescription );
		}
	}

	void OnClickContinue(){
		// Attach THIS info.
		ScenarioCtrl.Instance.saveInfo = info;
		ScenarioCtrl.Instance.fileName = fileName;

		// Save.
		if( SystemUIEvent.saveMode ){

#if false				
// Native Dialog.			
			if( m_FileExists ){
				Debug.Log( "SaveFileExists");
				if( Application.isEditor ){
					DoSave( true );
				}
				else{
					DialogManager.Instance.ShowSelectDialog( "Overwrite File" , "Are you sure you want to overwrite file ?" , DoSave );
				}
			}
			else{
#endif				
				DoSave( true );
//			}

//			Application.CaptureScreenShot(  );
//			CaptureScreenShot.StartCapture( fileName );
		}
		// Load.
		else{
			ViNoEventManager em = ViNoEventManager.Instance;

			// Go to Level and Continue.
			ScenarioCtrl.Instance.DoContinue();

			// Deactive SaveLoad and Title Panels.
			em.TriggerEvent( "ToggleActiveSaveLoadPanel" );

			em.TriggerEvent( "OnClickLoadItem" );
		}
	}

}
